package wch;

import robocode.HitByBulletEvent;
import robocode.HitRobotEvent;
import robocode.HitWallEvent;
import robocode.Robot;
import robocode.ScannedRobotEvent;

/**
 * Implements the Darumble competitive robot, which competes with other robots to win the round with
 * evasive maneuvering and basic offense.
 * 
 * @author Christopher Wilson
 */
public class DaRumble extends Robot {
  private byte updateDirection = 1;
  private String enemy = null;

  /**
   * Continuously tries to pick up an enemy robot on its scanner.
   * 
   */
  public void run() {

    while (true) {
      turnRadarRight(360);
    }
  }

  /**
   * Fires at any robots it finds with a power that is based on distance from target.
   * 
   * @param e contains information about the enemy robot, e.g. its location
   */
  @Override
  public void onScannedRobot(ScannedRobotEvent e) {
    if (setEnemy(e.getName())) {
      turnRadarRight(getHeading() - getRadarHeading() + e.getBearing());
    }
    turnGunRight(getHeading() - getGunHeading() + e.getBearing());
    fire(Math.floor(400 / e.getDistance()));
    scan();
  }

  /**
   * Moves the robot away from the wall.
   * @param e contains information about our robot, e.g. its location.
   */
  
  
  @Override
  public void onHitWall(HitWallEvent e) {
    turnRight(e.getBearing() + 180);
    updateDirection *= -1;
    ahead(updateDirection * getBattleFieldWidth() / 2);
  }

  /**
   * Moves the robot out of the enemy robot's path if rammed by the robot.
   *
   *@param e contains information about our robot, e.g. its location.
   */
  @Override
  public void onHitRobot(HitRobotEvent e) {
    turnRight(e.getBearing() + 45);
    updateDirection *= -1;
    ahead(updateDirection * getBattleFieldHeight() / 2);
  }

  /**
   * Moves out of enemy robot's line of fire if hit by one of their bullets.
   *
   *@param e contains information about our robot, e.g. its location.
   */
  @Override
  public void onHitByBullet(HitByBulletEvent e) {
    turnLeft(90 - e.getBearing());
    ahead(updateDirection * getBattleFieldWidth() / 4);
  }

  /**
   * helper method that sets an enemy robot and tracks it.
   * 
   * 
   * 
   * @param e Sets the string for the enemy.
   * @return returns string with enemy name.
   */
  public boolean setEnemy(String e) {
    boolean enemyString = false;
    if (enemy == null) {
      enemy = e;
      enemyString = true;
    }
    return enemyString;
  }
}
